//
//  DBGlucose.m
//  CareRingApp
//
//  Created  on 2025/2/11.
//
    

#import "DmdbGlucose.h"
#import "ConfigModel.h"
@implementation DmdbGlucose


NSString * const TABLE_NM_GLU = @"table_glu_res";




-(BOOL)isNormal
{

//    return self.current_level.intValue == 0;//self.upper_bound.floatValue < 6.1f;
    return  self.upper_bound.floatValue <= 6.9f;

}


-(UIColor *)riskColor
{
    return [[self class] colorWithRisk:self.current_level.intValue];
}

+(UIColor *)colorWithRisk:(int)current_level
{
    UIColor * color = nil;
    switch (current_level) {
        case 0:
        {
            color = [UIColor colorWithRed:166/255.0 green:166/255.0 blue:166/255.0 alpha:1.0]; //灰色
        }
            break;
        case 1:
        {
            color = [UIColor colorWithRed:21/255.0 green:168/255.0 blue:158/255.0 alpha:1.0]; //蓝绿色

        }
            break;
            
        case 2:
        {
            color = [UIColor colorWithRed:254/255.0 green:212/255.0 blue:1/255.0 alpha:1.0]; //金黄

        }
            break;
            
        case 3:
        {
            color = [UIColor colorWithRed:248/255.0 green:149/255.0 blue:33/255.0 alpha:1.0]; //橙色

        }
            break;
        case 4:
        {
            color = [UIColor colorWithRed: 138/255.0 green:7/255.0 blue:7/255.0 alpha:1.0]; //红色
        }
            break;
            
        default:
            break;
    }
    
    return color;
}

-(NSString *)meaTimeTypeString
{

    if (self.fasting) {
        return @"Fasting for 8 hours";
    }
    if(self.within_2hrs_meal) {
        return @"Within 2 hours meal";

    }
    return nil;
}



-(NSString *)riskTitle
{
    return [[self class] titleWithRisk:self.current_level.intValue];
}
+(NSString *)titleWithRisk:(int)current_level
{
    
    /*
     "glu_cur_0" = ;
     "glu_cur_1" = ;
     "glu_cur_2" = ;
     "glu_cur_3" =
     "glu_cur_4" =
     */

    NSString * title = nil;
    switch (current_level) {
        case 0:
        {
            title = @"No risk assessment available.";
        }
            break;
        case 1:
        {
            title = @"Low";

        }
            break;
            
        case 2:
        {
            title = @"Marginal";

        }
            break;
            
        case 3:
        {
            title = @"Moderate";

        }
            break;
        case 4:
        {
            title = @"High";
        }
            break;
            
        default:
            break;
    }
    
    return title;
}


-(NSString *)riskTip
{

    if ([self.risk_code isEqualToString:@"RISK000"])
    {
        return  @"Please measure your blood glucose on at least 3 days within 7 days to assess your risk of diabetes.";
    }
   
    if ([self.risk_code isEqualToString:@"RISK001"])
    {
        return  @"Regular brisk walking can help you maintain a healthy weight and lose body fat.";
    }
    
    if ([self.risk_code isEqualToString:@"RISK002"] || [self.risk_code isEqualToString:@"RISK003"])
    {
        return  @"The amount of sugar in a person’s blood is not constant. It varies depending on what the person has eaten that day, down to the specific kind of carbohydrate (For instance, rice, bread and potatoes are all different kinds of carbohydrates, yet people often digest them differently).";
    }
    
    if ([self.risk_code isEqualToString:@"RISK004"])
    {
        return  @"Prediabetes is a condition when your blood sugar levels are higher than normal but are not high enough that you are considered as being diabetic. It is reversible with the right lifestyle changes like following a healthy diet and engaging in sufficient physical activity daily.";;
    }
    if ([self.risk_code isEqualToString:@"RISK005"])
    {
        return  @"90% of the prediabetic individuals are not aware of their conditions as there is no obvious symptom, and 70% of them will eventually progress to diabetics.";
    }
    
    return nil;
}

-(NSString *)riskInsight
{

    if ([self.risk_code isEqualToString:@"RISK000"])
    {
        return  nil;
    }
   
    if ([self.risk_code isEqualToString:@"RISK001"])
    {
        return  @"You have low risk of developing Type 2 Diabetes.\r\n Your blood glucose level is consistently normal. Keep up your healthy lifestyle and continue to monitor your blood glucose level occasionally.";
    }
    
    if ([self.risk_code isEqualToString:@"RISK002"] || [self.risk_code isEqualToString:@"RISK003"])
    {
        return  @"You have marginal risk of developing Type 2 Diabetes.\r\n There is low occurrence of your blood glucose level that is higher than normal. This is not unusual as blood glucose level can vary depending on a variety of factors. Continue to monitor your blood glucose level closely to ensure that the risk level does not elevate over time.";
    }
    
    if ([self.risk_code isEqualToString:@"RISK004"])
    {
        return  @"You have moderate risk of developing Type 2 Diabetes.\r\n Your blood glucose level is higher than normal most of the time. A consultation with your medical doctor for further checks is advised.";
;
    }
    if ([self.risk_code isEqualToString:@"RISK005"])
    {
        return  @"You have high risk of developing Type 2 Diabetes.\r\n Your blood glucose level is consistently higher than normal. An immediate consultation with your medical doctor for further checks is advised.";
    }
    
    return nil;
}





// glu_risk_1
-(NSString *)riskIconName
{
    return [[self class] iconNameWithRisk:self.current_level.intValue];;
}
+(NSString *)iconNameWithRisk:(int)current_level
{

    NSString * color = nil;
    switch (current_level) {
        case 0:
        {
            color = @"glu_risk_0";
        }
            break;
        case 1:
        {
            color = @"glu_risk_1";

        }
            break;
            
        case 2:
        {
            color = @"glu_risk_2";

        }
            break;
            
        case 3:
        {
            color = @"glu_risk_3";

        }
            break;
        case 4:
        {
            color = @"glu_risk_4";
        }
            break;
            
        default:
            break;
    }
    
    return color;
}





-(BOOL)parseFromNetDict:(NSDictionary *)dict
{
  
    BOOL succ = YES;
    NSNumber *state = dict[@"state"];
    if (state.intValue != 0 || !state) {
        succ = NO;
        return succ;
    }
    NSDateFormatter *fmt = [NSDateFormatter new];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    NSDictionary *measurement_data = dict[@"data"][@"measurement_data"];
    
    NSString * end_time = measurement_data[@"end_time"];
    NSString * start_time = measurement_data[@"start_time"];
    
    self.start_time = @( (NSUInteger)([fmt dateFromString:start_time].timeIntervalSince1970) );
    
    self.end_time = @( (NSUInteger)([fmt dateFromString:end_time].timeIntervalSince1970) );

    self.lower_bound = measurement_data[@"lower_bound"]; // 下限
    self.upper_bound = measurement_data[@"upper_bound"]; // 上限
    
    self.within_2hrs_meal = [measurement_data[@"within_2hrs_meal"] boolValue];
    self.fasting = [measurement_data[@"fasting"] boolValue];
    
    // 风险
    NSDictionary *risk_data = dict[@"data"][@"risk_data"];
    self.current_level = risk_data[@"current_level"];
    self.risk_code = risk_data[@"risk_code"];
    
    if (!self.lower_bound || !self.upper_bound || !self.risk_code) {
        return NO;
    }
    
    return succ;
}



@end
